<?php
namespace Admin\Controller;
class LoginController extends BaseController {
    public function __construct($check_login = false) {
        parent::__construct($check_login);
    }

    public function index() {
        //判断客户端是否已经登录
        if (isWebLogin()) {
            //重定向到指定的URL地址
            redirect('/shuidifudao/admin/index/index/', 0, '页面跳转中...');
            return;
        }
        //否则走登录逻辑
        $this->display();
    }

    public function postData() {
        if (!$_POST) {
            return;
        }
        $username = I('username');
        $password = I('password');
        $code = I('code');
        if (!$username || !$password) {
            $this->error('用户名或密码不能为空', 'index', false);
            return;
        }
        if (!$code) {
            $this->error('验证码不能为空', 'index', false);
            return;
        }
        //校验验证码
//        if (!check_verify($code)) {
//            $this->error("亲，验证码输错了哦！", 'index', false);
//            return;
//        }
        //校验用户名密码
        $sql = M('admin_user');
        $user = $sql->where("username='{$username}'")->field('uid,username,password,salt')->find();
        if (!$user) {
            $this->error("对不起，该用不户存在！", 'index', false);
            return;
        }
        if (jiami($password . $user['salt']) != $user['password']) {
            $this->error("密码错误", 'index', false);
            return;
        }
        //为客户端设置cookie
        cookie('username', $username);
        cookie('uid', $user['uid']);
        cookie('key', jiami($username . $user['uid'] . C('COOKIE')));
        redirect('index/index', 0);
    }

    public function logout() {
        //为客户端清空cookie
        cookie('username', null);
        cookie('uid', null);
        cookie('key', null);
        redirect('/shuidifudao/admin/login/index/', 0, '页面跳转中...');
    }
}